package com.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shop.common.TokenUtil;
import com.shop.common.result.Result;
import com.shop.common.result.util.ResultModel;
import com.shop.dto.CensusClientBaseOrderDto;
import com.shop.entity.BaseOrderDetailDo;
import com.shop.entity.BaseOrderDo;
import com.shop.entity.ProductCommentDo;
import com.shop.mapper.BaseOrderDetailDoMapper;
import com.shop.mapper.BaseOrderDoMapper;
import com.shop.mapper.ProductCommentDoMapper;
import com.shop.service.BaseOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 订单表 服务实现类
 * </p>
 *
 * @author 靳 旺
 * @since 2021-01-29
 */
@Service
public class BaseOrderServiceImpl extends ServiceImpl<BaseOrderDoMapper, BaseOrderDo> implements BaseOrderService {

    @Autowired
    private BaseOrderDetailDoMapper baseOrderDetailDoMapper;

    @Autowired
    private ProductCommentDoMapper productCommentDoMapper;

    @Override
    public ResultModel censusClientBaseOrder() throws Exception {
        QueryWrapper<BaseOrderDo> baseOrderDoQueryWrapper = new QueryWrapper<>();
        baseOrderDoQueryWrapper.eq("fk_client_id", TokenUtil.getUserId());
        baseOrderDoQueryWrapper.eq("is_del", "n");
        CensusClientBaseOrderDto censusClientBaseOrderDto = new CensusClientBaseOrderDto();
        List<BaseOrderDo> baseOrderDos = super.list(baseOrderDoQueryWrapper);
        if (baseOrderDos == null || baseOrderDos.size() <= 0) {
            censusClientBaseOrderDto.setToBeDelivered(0);
            censusClientBaseOrderDto.setToBeEvaluated(0);
            censusClientBaseOrderDto.setToBePaid(0);
            censusClientBaseOrderDto.setToBeReceived(0);
            return Result.successResultModel(censusClientBaseOrderDto);
        }
        //待发货
        List<BaseOrderDo> toBeDelivered = baseOrderDos.stream().filter(k -> k.getShippingTime() == null).collect(Collectors.toList());
        List<Long> orderIds = baseOrderDos.stream().map(BaseOrderDo::getPkBaseOrderId).collect(Collectors.toList());
        //待付款
        List<BaseOrderDetailDo> baseOrderDetailDos = baseOrderDetailDoMapper.selectBatchIds(orderIds);
        List<BaseOrderDetailDo> toBePaid = baseOrderDetailDos.stream().filter(k -> k.getIsPayment().equals("n")).collect(Collectors.toList());
        //待收货
        List<BaseOrderDetailDo> toBeReceived = baseOrderDetailDos.stream().filter(k -> k.getSingFor().equals("n")).collect(Collectors.toList());
        //待评价
        QueryWrapper<ProductCommentDo> productCommentDoQueryWrapper = new QueryWrapper<>();
        productCommentDoQueryWrapper.eq("fk_client_id", TokenUtil.getUserId());
        baseOrderDoQueryWrapper.eq("is_del", "n");
        List<ProductCommentDo> productCommentDos = productCommentDoMapper.selectList(productCommentDoQueryWrapper);
        //已收货
        List<BaseOrderDetailDo> received = baseOrderDetailDos.stream().filter(k -> k.getSingFor().equals("y")).collect(Collectors.toList());
        int toBeEvaluated =(received.size() - productCommentDos.size());
        censusClientBaseOrderDto.setToBeDelivered(toBeDelivered.size());
        censusClientBaseOrderDto.setToBeEvaluated(toBeEvaluated);
        censusClientBaseOrderDto.setToBePaid(toBePaid.size());
        censusClientBaseOrderDto.setToBeReceived(toBeReceived.size());
        return Result.successResultModel(censusClientBaseOrderDto);
    }
}
